const path = require('path');
const webpack = require('webpack');
const projectRoot = path.resolve(__dirname, '..');
 const ExtractTextPlugin = require('extract-text-webpack-plugin');

var jquery=path.resolve(__dirname,'../src/lib/jquery.min.js')

csspath="../dist/css/"

var node_env=process.env.node_env.trim() || 'development';
module.exports = {
  target: 'node', // !different
  entry: {
    'bundle.server':[path.join(projectRoot, 'src/server.js')]
  },
  output: {
    libraryTarget: 'commonjs2', // !different
    path: path.join(projectRoot, 'dist'),
    filename: 'bundle.server.js',
  },
  externals: Object.keys(require('../package.json').dependencies),
  module: {
    rules: [
      { test: /iview.src.*?js$/, use: ['babel'] },
      {
        test: /muse-ui.src.*?js$/,
        use: ['babel']
      },
      {
        test: /\.vue$/,
        use:[
              {
                loader:'vue-loader',
                options: {//.vue 内css 打包
                  loaders: {
                      css: 'css-loader',
                      lcss:['css-loader','less-loader']
                  }
                }
              } 
        ],
        // query: {
        //   presets: ['es2015']
        // }
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: [{
          loader: "babel-loader",
          options: { presets: ["es2015"] }
        }]
      },{
        test:/\.css$/,
        //use:['style-loader','css-loader']
        use:ExtractTextPlugin.extract({
          fallback:'style-loader',
          use:'css-loader',
        })
      },{
        test:/\.(less|lcss)$/,
        //use:['style-loader','css-loader','less-loader']
        use:ExtractTextPlugin.extract({
          fallback:'style-loader',
          use:['css-loader','less-loader'],
        })
      },{
        test: /\.(png|jpg|jpeg|gif)(\?.+)?$/,
        use:['file-loader?limit=8192&name=images/[name].[ext]'] 
      },
      {
        test: /\.(eot|ttf|woff|woff2|svg|svgz)(\?.+)?$/,
        use:['file-loader?limit=8192&name=fonts/[name].[ext]'] 
      },
      {test: jquery, use: "expose-loader?$"},
      {test: jquery, use: "expose-loader?jQuery"},
    ]
  },
  resolve: {
        extensions: ['.js',  '.css', '.less'],
        //别名设置,主要是为了配和webpack.ProvidePlugin设置全局插件;
        alias: {
            //设置全局jquery插件;
            'jquery':jquery, //绝对路径;
            'platform':node_env,
            'muse-components': 'muse-ui/src',
            'vue': 'vue/dist/vue.js'
        }
  },plugins: [
    
    // 提取 CSS 到单独的文件中
    new ExtractTextPlugin({filename:csspath+'[name].css',allChunks: true}),//设置生成目录和名，热更新时src 为设置的
    
    new webpack.ProvidePlugin({ 
        "$": 'jquery'
    }),
    
  ]
};
